package cz.muni.fi.pa165.soap.client;

import cz.muni.fi.pa165.soap.client.editPannel.TabButton;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author shifty
 */
public class EditFrame extends JFrame {
    
    private ResourceBundle rb;
    private JTabbedPane tabPane;
    
    public EditFrame(ResourceBundle rb) {
        this.rb=rb;
        init();
    }
    
    private void init() {
        setLayout(new BorderLayout());
        
        tabPane = new JTabbedPane();
        add(tabPane);
        tabPane.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if(tabPane != null || tabPane.getTabCount() != 0) {
                    if(tabPane.getSelectedComponent() != null) {
                        String title = ((JPanel)tabPane.getSelectedComponent()).getToolTipText();
                        setTitle(title);
                        pack();
                    }
                }
            }
        });
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentHidden(ComponentEvent e) {
                if(tabPane != null) {
                    tabPane.removeAll();
                }
            }
        });
    }
    
    public void addTab(final JPanel panel) {
        tabPane.add(panel.getToolTipText(),panel);
        TabButton tb = new TabButton(panel.getToolTipText());
        tb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tabPane.remove(panel);
                if(tabPane.getTabCount() == 0) {
                    setVisible(false);
                }
            }
        });
        tabPane.setTabComponentAt(tabPane.indexOfComponent(panel),tb);
        if(!isVisible()) {
            setVisible(true);
        }
        pack();
    }
    
    public void removeTab(Long id) {
        
    }
    
}
